package book.controller;

import book.entity.*;
import book.service.RoleServices;
import book.service.UserServices;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserContrller {

    @Autowired
    UserServices userServices;

    @Autowired
    RoleServices roleServices;

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        List<role> roles = new ArrayList<role>();
        roles = roleServices.getAll();
        request.setAttribute("roles",roles);
        return "guanliyuan/guanliyuan";
    }

    @RequestMapping(value = "/all",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> cha(@RequestParam("offset") int offset, @RequestParam("limit") int limit, User condition){
        List<User> list=userServices.getAll(offset,limit,condition);
        Map<String, Object> map = new HashMap<String,Object>();
        int total = userServices.getCount();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toadd(HttpServletRequest request){
        List<role> list=roleServices.getAll();
        request.setAttribute("ShenFen",list);
        return "guanliyuan/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add(HttpServletRequest request, @RequestParam("bookImg")MultipartFile multipartFile,User dz){
        String oldFileName = multipartFile.getOriginalFilename();
        Map<String,Object> map = new HashMap<String,Object>();
            //获取系统时间差(现在-1970-1-1)毫秒
            //String newFileName = oldFileName+"_"+System.currentTimeMillis();
            //后缀
            String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
            String newFileName = oldFileName.substring(0,oldFileName.lastIndexOf("."))+"_"+System.currentTimeMillis()+suffix;
            String url = request.getServletContext().getRealPath("/upload");
            File targetFile = new File(url,newFileName);
            try {
                multipartFile.transferTo(targetFile);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            dz.setTupian("upload/"+newFileName);
            System.out.println("dfsfsd "+JSON.toJSONString(dz));
            userServices.insert(dz);
            map.put("result",true);
        return map;
    }



    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(@RequestParam("id") int id){
        Map<String, Object> map = new HashMap<String,Object>();
        userServices.delete(id);
        map.put("result",true);
        return map;
    }


    @RequestMapping(value = "/batchDel",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> todelete(IdsModel idsModel){
        Map<String, Object> map = new HashMap<String,Object>();
        System.out.println(idsModel.getIds() != null);
//        userServices.todel(idsModel.getIds());
        map.put("result",true);
        return map;
    }


    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public String update(@RequestParam("id") int id,HttpServletRequest request){
        User d=userServices.getById(id);
        List<role> shenfen = roleServices.getAll();
        request.setAttribute("duixiang",d);
        request.setAttribute("list",shenfen);
        return "guanliyuan/update";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> toupdate(HttpServletRequest request, @RequestParam("bookImg")MultipartFile multipartFile,User dz){
//        Map<String, Object> map = new HashMap<String,Object>();
//        userServices.update(dz);
//        map.put("result",true);

        //返回相对路径的真实路径  application.getRealPath("/upload");
        //获取原始上传文件名
        String oldFileName = multipartFile.getOriginalFilename();
        Map<String,Object> map = new HashMap<String,Object>();
        if(oldFileName!=null && oldFileName!="") {
            //获取系统时间差(现在-1970-1-1)毫秒
            //String newFileName = oldFileName+"_"+System.currentTimeMillis();
            //后缀
            String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
            String newFileName = oldFileName.substring(0,oldFileName.lastIndexOf("."))+"_"+System.currentTimeMillis()+suffix;
            String url = request.getServletContext().getRealPath("/upload");
            File targetFile = new File(url,newFileName);
            try {
                multipartFile.transferTo(targetFile);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            dz.setTupian("upload/"+newFileName);
            System.out.println("dfsfsd "+JSON.toJSONString(dz));
            userServices.update(dz);

            map.put("result",true);
        }else{
            userServices.update(dz);
            map.put("result",true);
        }
        return map;
    }
}
